#!/bin/sh
### BEGIN INIT INFO
# Provides:          gitea_auxd
# Required-Start:    $syslog $network $gitea
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Gitea app.ini hostname set to local ip daemon.
# Description:       Gitea app.ini hostname set to local ip daemon.
### END INIT INFO

# Author: Cracy3m

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
DESC="Gitea app.ini hostname daemon"

NAME=gitea_auxd
PIDFILE=/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON=/usr/local/bin/gitea_restart

USER=root

STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/1/KILL/5}"  #超时强制停止时间 格式 信号/超时秒数

CMDGETIP='ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '\''{print $2}'\'' | tr -d "addr:"'

SLEEPT=3

GiteaHostNameOrg=localhost  #原本的gitea主机名
GiteaHostName=           #这里留空表示用局域网的ip作为主机名
GiteaINIorg=app.ini.org #原始的配置文件，修改localhost字符串后覆盖到gitea的调用的配置文件
GiteaINI=app.ini
GiteaINIPath=/etc/gitea
GiteaINIFile=${GiteaINIPath}/${GiteaINI}
GiteaINIFileOrg=${GiteaINIPath}/${GiteaINIorg}


# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Exit if the app.ini shadow file is not exists
[ -e "$GiteaINIFileOrg" ] || exit 0

do_first_app_ini_ch(){
    # If the host ip or $GiteaHostName is valid, change app.ini.
    IPaddr=$(eval $CMDGETIP)
    IPaddrLen=${#IPaddr}

    if [ $IPaddrLen -gt 3 -o -n "${GiteaHostName}"]; then 
        service gitea stop 
        sleep $SLEEPT
        
        cp -f $GiteaINIFileOrg $GiteaINIFile

        if [ -n "${GiteaHostName}" ]; then 
        sed -i $GiteaINIFile -e "s/${GiteaHostNameOrg}/${GiteaHostName}/gi" 
        elif [ -n "${IPaddr}" ]; then 
        sed -i $GiteaINIFile -e "s/${GiteaHostNameOrg}/${IPaddr}/gi" 
        fi
        
        service gitea start 
    fi
}

do_start()
{
    start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON #sh -- $DAEMON
    #sh -c "USER=$USER HOME=/home/$USER GITEA_WORK_DIR=$WORKINGDIR start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --chdir $WORKINGDIR --chuid $USER --exec $DAEMON -- $DAEMON_ARGS"
}

do_stop()
{
    start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME --oknodo --retry=$STOP_SCHEDULE
    rm -f $PIDFILE
}

do_status()
{
    if [ -f $PIDFILE ]; then
        if kill -0 $(cat "$PIDFILE"); then
            echo "$NAME is running, PID is $(cat $PIDFILE)"
        else
            echo "$NAME process is dead, but pidfile exists"
        fi
    else
        echo "$NAME is not running"
    fi
}

case "$1" in
    start)
        echo "Starting $DESC" "$NAME"
        do_first_app_ini_ch
        do_start
        ;;
    stop)
        echo "Stopping $DESC" "$NAME"
        do_stop
        ;;
    status)
        do_status
        ;;
    restart)
        echo "Restarting $DESC" "$NAME"
        do_stop
        do_start
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
        exit 2
        ;;
esac

exit 0
